<?php

namespace util;

/**
 * 公共函数类
 *
 * @author fuyou<fuyou@dangdang.com>
 * @since May 22, 2015 2:41:33 PM
 * @version 2.0
 * @copyright (c) 2015 , DANG
 *           
 */
class ComFunc {
    /**
     * 获取毫秒数
     *
     * @return Ambigous <>
     * @since May 20, 2015 5:12:09 PM
     * @access
     *
     */
    public static function getMillisecond() {
        list ( $usec, $sec ) = explode ( " ", microtime () );
        $millSecond = (( float ) $usec + ( float ) $sec) * 1000;
        return $millSecond;
    }
    /**
     * 转换XML文件编码，主要针对GBK[2312]或者GBK编码的进行处理, 同时去掉可能影响XML解析错误的字符
     *
     * @param string $xml_string
     *            要转换的字符串
     * @param string $encode
     *            要转换为的编码,默认为UTF-8
     * @access public
     * @return string
     */
    public static function convertXmlEncode($xml_string, $encode = 'UTF-8') {
        if (empty ( $xml_string )) {
            return '';
        }
        $now_encode = self::detectEncode ( $xml_string );
        $xml_string_arr = explode ( '?>', $xml_string );
        if (preg_match ( "/(^<\?xml.*?encoding=\")GB2312|GBK(.*)/i", trim ( $xml_string_arr [0] ) )) {
            $xml_string_arr [0] = preg_replace ( "/(^<\?xml.*?encoding=\")GB2312|GBK(.*)/i", '${1}' . $encode . '${2}', trim ( $xml_string_arr [0] ) ); // 如果头部声明是GB2312则替换为$encode
        }
        if (empty ( $xml_string_arr )) {
            return '';
        }
        $xml_string = implode ( '?>', $xml_string_arr );
        $xml_string = iconv ( $now_encode, 'UTF-8//IGNORE', $xml_string );
        return $xml_string;
    }
    /**
     * 检测字符串当前编码
     *
     * @param string $string
     *            需要检测的字符串
     * @access public
     * @return string 字符编码:UTF-8,...
     */
    public static function detectEncode($string) {
        $encodes = array (
                'ASCII',
                'UTF-8',
                'GBK',
                'GB2312',
                'CP936' 
        );
        $now_encode = mb_detect_encoding ( $string, $encodes );
        if (false === $now_encode) { // user reco 为什么返回的东西就不能分析出编码呢...
            return 'CP936';
        }
        return $now_encode;
    }
    /**
     * 写日志
     *
     * @param mixed $content            
     * @param string $path            
     * @since Jun 11, 2015 10:09:04 AM
     * @access
     *
     */
    public static function writeLog($content, $path) {
        if (! is_dir ( $path )) {
            mkdir ( $path );
        }
        $date = date ( 'Y-m-d' );
        $file = $path . "{$date}.log";
        if (! is_file ( $file )) {
            touch ( $file );
            chmod ( $file, 0777 );
        }
        if (file_exists ( $file )) {
            $file_handle = @fopen ( $file, 'a+' );
            flock ( $file_handle, LOCK_EX );
            if ($file_handle) {
                $file_content = $content . "\r\n";
                @fwrite ( $file_handle, $file_content );
                flock ( $file_handle, LOCK_UN );
            }
            fclose ( $file_handle );
        }
    }
}
    